package com.adou.service.impl;

import com.adou.common.dto.FlowMainOutDto;
import com.adou.common.mapper.FlowDirectionMapper;
import com.adou.common.mapper.FlowMainFileMapper;
import com.adou.common.mapper.FlowMainMapper;
import com.adou.common.mapper.FlowTaskMapper;
import com.adou.common.model.FlowMain;
import com.adou.common.model.FlowMainFile;
import com.adou.service.FlowMainService;
import com.adou.service.impl.base.BaseServiceImp;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class FlowMainServiceImpl extends BaseServiceImp<FlowMain> implements FlowMainService {

    @Autowired
    private FlowMainMapper flowMainMapper;

    @Autowired
    private FlowTaskMapper flowTaskMapper;

    @Autowired
    private FlowDirectionMapper flowDirectionMapper;

    @Autowired
    private FlowMainFileMapper flowMainFileMapper;

    @Transactional(readOnly = true)
    @Override
    public Map<String, Object> pageList(int pageNo,int pageSize,Map<String, Object> map) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            Page<?> page = PageHelper.startPage(pageNo,pageSize);
            List<FlowMain> list = flowMainMapper.pageList(map);
            if(list!=null && list.size()>0){
                List<FlowMainOutDto> res = list.stream().map(FlowMainOutDto::new).collect(Collectors.toList());
                Map<String, Object> data = new HashMap<>();
                data.put("totalpage", page.getPages());
                data.put("totalcount", page.getTotal());
                data.put("list", res);
                resultMap.put("data", data);
                resultMap.put("msg", "列表获取成功！");
            }else {
                resultMap.put("result", 101);
                resultMap.put("msg", "列表获取为空！");
            }
        }catch (Exception e){
            resultMap.put("result", 102);
            resultMap.put("msg", "列表获取报错！");
            logger.error(e.toString());
        }
        return resultMap;
    }

    @Override
    public int update(FlowMain o) {
        FlowMainFile flowMainFile = new FlowMainFile();
        flowMainFile.setIfmid(o.getId());
        List<FlowMainFile> flowMainFileList = flowMainFileMapper.find(flowMainFile);
        if(flowMainFileList.size()>0){
            FlowMain flowMain = new FlowMain();
            flowMain.setId(o.getId());
            List<FlowMain> flowMainList = flowMainMapper.find(flowMain);
            if(flowMainList.size()==1){
                flowMain = flowMainList.get(0);
                if(o.getCformident() != null && !flowMain.getCformident().equals(o.getCformident())){
                    return -2;
                }
                if(o.getCflowids() != null && !flowMain.getCflowids().equals(o.getCflowids())){
                    return -3;
                }
                if(o.getCflowusers() != null && !flowMain.getCflowusers().equals(o.getCflowusers())){
                    return -3;
                }
            }
            else {
                return -1;
            }
        }
        return super.update(o);
    }

    @Override
    public int deleteBatch(List<FlowMain> o) {
        FlowMainFile flowMainFile = new FlowMainFile();
        List<FlowMainFile> flowMainFileList;
        for (FlowMain flowMain : o) {
            flowMainFile.setIfmid(flowMain.getId());
            flowMainFileList = flowMainFileMapper.find(flowMainFile);
            if(flowMainFileList.size()>0){
                return -1;
            }
        }
        flowTaskMapper.deleteByIfmid(o);
        flowDirectionMapper.deleteByIfmid(o);
        return super.deleteBatch(o);
    }
}
